﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SMG.Domain.Repository.System;
using SMG.Application.DTO.System;
using SMG.Application.Extension;
using SMG.ServiceContract.System;
using Infrastructure.Core.Common;
using SMG.Domain.Repository;

namespace SMG.Application.System
{
    /// <summary>
    /// 用户服务实现
    /// </summary>
    public class UserServiceImpl : ApplicationService, IUserService
    {
        private IUserRepository userRepository;

        public UserServiceImpl(IRepositoryContext context,
                                IUserRepository userRepository)
            : base(context)
        {
            this.userRepository = userRepository;
            this.userRepository.SetContext(context);
        }

        #region IUserService Members

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        public bool Register(UserDto userDto)
        {
            if (userDto == null)
                throw new ArgumentNullException("userDto");

            //if (userRepository.UserNameExist(userDto.UserName))
            //    throw new Exception("重复的用户名");

            userRepository.Create(userDto.ConvertToUser());
            return Context.Commit();
        }
        /// <summary>
        /// 校验用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Boolean ValidateUser(String userName, String password) {
            if (String.IsNullOrEmpty(userName))
                throw new ArgumentNullException("username");
            if (String.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            return userRepository.CheckPassword(userName, password);
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        public bool UpdateUser(UserDto userDto)
        {
            userRepository.Update(userDto.ConvertToUser());
            return base.Context.Commit();
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool DeleteUser(long userId)
        {
            userRepository.Delete(userRepository.GetByKey(userId));
            return base.Context.Commit();
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserDto GetUser(long userId)
        {
            return userRepository.GetByKey(userId).ConvertToUserDto();
        }
        /// <summary>
        /// 启用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Boolean EnableUser(long userId)
        {
            var user = userRepository.GetByKey(userId);
            if (user == null)
                throw new ArgumentException("没有此用户信息userid");

            user.Enable();
            userRepository.Update(user);
            return userRepository.Context.Commit();
        }
        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Boolean DisableUser(long userId)
        {
            var user = userRepository.GetByKey(userId);
            if (user == null)
                throw new ArgumentException("没有此用户信息userid");

            user.Disable();
            userRepository.Update(user);
            return userRepository.Context.Commit();
        }
        /// <summary>
        /// 查询所有用户列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<UserDto> QueryAll()
        {
            return userRepository.QueryAll().ToList().ConvertToUserDto();
        }
        /// <summary>
        /// 分页查询用户信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResult<UserDto> QueryUserByPage(Int32 pageIndex, Int32 pageSize) {
            var result = userRepository.QueryByPage(u=>u.ID, SortOrder.Unspecified, pageSize, pageIndex);
            return new PageResult<UserDto>()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                TotalRecords = result.TotalRecords,
                Data = result.Data == null ? null : result.Data.ConvertToUserDto()
            };
        }

        #endregion

    }
}
